package com.dianxin.farm.api;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dianxin.farm.entity.GoodsInfo;
import com.dianxin.farm.entity.GoodsInfoImage;
import com.dianxin.farm.filter.AuthorCheck;
import com.dianxin.farm.request.common.IdRequest;
import com.dianxin.farm.request.goods.QueryGoodsRequest;
import com.dianxin.farm.response.goods.GoodsDetailResponse;
import com.dianxin.farm.response.goods.GoodsPageResponse;
import com.dianxin.farm.service.IGoodsInfoImageService;
import com.dianxin.farm.service.IGoodsInfoService;
import com.dianxin.farm.util.exception.UserOperationException;
import com.dianxin.farm.util.util.StringUtil;
import com.dianxin.farm.util.web.ApiResponse;
import com.dianxin.farm.util.web.ApiResponseUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 商品基础信息表 前端控制器
 * </p>
 *
 * @author xukai
 * @since 2020-09-14
 */
@RestController
@RequestMapping(value = "/goodsInfo", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
@Api(description = "服务商品控制器，订单购买的是此类商品")
public class GoodsInfoController {

    @Resource
    private IGoodsInfoService goodsInfoService;
    @Resource
    private IGoodsInfoImageService goodsImageService;

    @ApiOperation("商品列表分页")
    @RequestMapping(value = "/goodsPages")
    @ResponseBody
    @AuthorCheck
    @Transactional
    public ApiResponse<PageInfo<GoodsPageResponse>> goodsPages(@Valid @RequestBody QueryGoodsRequest request) {
        String goodsName = request.getGoodsName();
        Long goodsCode = request.getGoodsCode();
        PageHelper.startPage(request.getPage(), 999);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.like(StringUtil.isNotBlank(goodsName), "goods_name", goodsName);
        wrapper.eq(goodsCode != null, "goods_code", goodsCode);
        wrapper.eq("shelf_status", 1);
        wrapper.orderByDesc("top_status");
        wrapper.orderByDesc("id");

        List<GoodsInfo> goodsInfos = goodsInfoService.list(wrapper);
        PageInfo goodsPage=new PageInfo(goodsInfos);


        List<GoodsPageResponse> responses = new ArrayList<>();
        if (goodsInfos != null && goodsInfos.size() > 0) {
            for (GoodsInfo goodsInfo : goodsInfos) {
                GoodsPageResponse response = new GoodsPageResponse();
                response.setGoodsInfo(goodsInfo);
                Integer goodsId = goodsInfo.getId();
                QueryWrapper imgeWr = new QueryWrapper();
                imgeWr.eq("goods_info_id", goodsId);
                List<GoodsInfoImage> images = goodsImageService.list(imgeWr);
                response.setGoodsImages(images);
                responses.add(response);
            }
        }

        PageInfo reslt=new PageInfo<>(responses);
        reslt.setTotal(goodsPage.getTotal());
        return ApiResponseUtils.success(reslt);
    }


    @ApiOperation("商品详情")
    @RequestMapping(value = "/goodsDetail")
    @ResponseBody
    @AuthorCheck
    @Transactional
    public ApiResponse<GoodsDetailResponse> goodsPages(@Valid @RequestBody IdRequest request) {

        Integer goodsId = request.getId();

        GoodsInfo goodsInfo = goodsInfoService.getById(goodsId);
        if (goodsInfo == null || goodsInfo.getShelfStatus() == 0) {
            throw new UserOperationException("商品不存在，或已下架");
        }
        GoodsDetailResponse response = new GoodsDetailResponse();
        response.setGoodsInfo(goodsInfo);

        QueryWrapper imgeWr = new QueryWrapper();
        imgeWr.eq("goods_info_id", goodsId);
        List<GoodsInfoImage> images = goodsImageService.list(imgeWr);
        response.setGoodsImages(images);

        return ApiResponseUtils.success(response);


    }
}